package ldkxingzhe.l365calendar.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import ldkxingzhe.l365calendar.R;
import ldkxingzhe.l365calendar.fragment.presenter.NoteEditPresenter;
import ldkxingzhe.l365calendar.widget.dialog.picker.DatePicker;

/**
 * 待办事项Fragment
 * Created by xingzhe on 16/1/28.
 */
public class NoteEditFragment extends AllEditFragmentBase
        implements NoteEditPresenter.PView, HandleFragmentResult{
    @SuppressWarnings("unused")
    private static final String TAG = "NoteEditFragment";

    private NoteEditPresenter presenter;

    // UI elements
    private EditText editText;
    private TextView tvDeadline, tvImportant;
    private View deadlineLayout, importantLayout;

    private String[] weekNames;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new NoteEditPresenter();
        presenter.setView(this);
        weekNames = getResources().getStringArray(R.array.week_names);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View resultView = inflater.inflate(R.layout.fragment_note_edit, container, false);
        editText = (EditText) resultView.findViewById(R.id.todo_edit_content);
        tvDeadline = (TextView) resultView.findViewById(R.id.btn_deadline);
        tvImportant = (TextView) resultView.findViewById(R.id.btn_priority);
        deadlineLayout = resultView.findViewById(R.id.time_layout);
        importantLayout = resultView.findViewById(R.id.priority_layout);
        initAdapter();
        return resultView;
    }

    private void initAdapter() {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                setIsChanged(true);
            }
        });
        deadlineLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v(TAG, "deadline layout was clicked");
                showTimeDialog();
            }
        });
        importantLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v(TAG, "important layout was clicked");
                presenter.onClickImportToDo();
            }
        });
    }

    private void showTimeDialog() {
        FragmentManager manager = getFragmentManager();
        DialogFragment dialogFragment = (DialogFragment) manager.findFragmentByTag("Dialog");
        if(dialogFragment == null){
            dialogFragment = DatePicker.newInstance(presenter.getLongTime());
        }else{
            dialogFragment.dismiss();
        }
        dialogFragment.setTargetFragment(this, 3);
        dialogFragment.show(manager, "Dialog");
    }

    @Override
    public void onSaveClick() {
        if(presenter.onSave()){
            getActivity().finish();
        }
    }

    @Override
    public void importToDoResource(int resourceId) {
        tvImportant.setCompoundDrawablesWithIntrinsicBounds(resourceId, 0, 0, 0);
        setIsChanged(true);
    }

    @Override
    public void setDeadlineText(String text) {
        tvDeadline.setText(text);
        setIsChanged(true);
    }

    @Override
    public String getDayName(int index) {
        return weekNames[index];
    }

    @Override
    public String getDeadLineText() {
        return tvDeadline.getText().toString();
    }

    @Override
    public String getContent() {
        return editText.getText().toString().trim();
    }

    @Override
    public void onHandleFragmentResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 3){
            presenter.onDeadlineSelect(resultCode, data);
            return;
        }
    }
}
